import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class ConsoleView extends JFrame {
	
	private JPanel backPanel;
	private OptionView optionPanel;
	private GraphView graphPanel;
	private Controller control;
	private InfoAnalysis info;
	
	public ConsoleView(Controller myControl, String title)
	{
		super(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		control=myControl;
		backPanel=new JPanel();
		optionPanel=new OptionView(control, this);
		graphPanel=new GraphView(control);
		backPanel.setLayout(new BoxLayout(backPanel, BoxLayout.Y_AXIS));
		//for SpringLayout: manager.putConstraint("WEST", (Component)graphPanel, 50, "EAST", (Component)graphPanel.getKeyPanel());
		
		//setPreferredSize(new Dimension(1000,1000));
		
		//this.setLayout(new FlowLayout());
		//backPanel.setLayout(new FlowLayout());
		//optionPanel.setAlignmentY(TOP_ALIGNMENT);
		//backPanel.setAlignmentY(TOP_ALIGNMENT);
		backPanel.setAlignmentY(LEFT_ALIGNMENT);
		
		
		backPanel.add(optionPanel);
		//backPanel.add(graphPanel);
		JPanel bottomPanel=new JPanel();
		bottomPanel.add(graphPanel);
		bottomPanel.add(graphPanel.getKeyPanel());
		backPanel.add(bottomPanel);
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
		
		
		setUpMenu();
		
		
		//add(backPanel);
		setContentPane(backPanel);
		
		
		this.validate();
		pack();
			
		
		//info=new InfoAnalysis();
		//info.testPlease();
		
		
	}
	
	public void setUpMenu()
	{
		JMenuBar menuBar=new JMenuBar();
		
		//--
		JMenu file=new JMenu("File");
		
		JMenuItem clearGraph=new JMenuItem("Clear Graph");
		clearGraph.addActionListener(graphPanel.getGraphClear());
		JMenuItem editGraph=new JMenuItem("Edit Graph Contents");
		editGraph.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				JFrame frame=new JFrame("Edit Graph Contents");
				frame.add(new EditContents(graphPanel, frame));
				frame.pack();
				frame.validate();
				frame.repaint();
				frame.setVisible(true);
				
			}
			
		});
		
		file.add(editGraph);
		file.add(clearGraph);
		//-- 
		JMenu about=new JMenu("About");
		JMenuItem developerInfo=new JMenuItem("Our Glorious Creators");
		
		/*JMenuItem sorting=new JMenuItem("Sorting Methods");
		JMenuItem searching =new JMenuItem("Searching Methods");

		sorting.addActionListener(new AboutListener("Sorting"));
		searching.addActionListener(new AboutListener("Searching"));*/
		
		JMenuItem animation=new JMenuItem("Sorting Method Animations");
		animation.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				AnimationControl animate=new AnimationControl(control);
				animate.standAlone();
				
			}
			
		});
		developerInfo.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				try{
				URL file = new File("sounds/62176__robinhood76__00504-brass-fanfare-4.wav").toURI().toURL();
				//URL file = new File("sounds/72129__kizilsungur__sweetalertsound5.wav").toURI().toURL();		
				AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);
				Clip clip = AudioSystem.getClip();
				clip.open(audioIn);
				clip.start();
				} catch (Exception f) {
				f.printStackTrace();
				} 
				JOptionPane.showMessageDialog(new JFrame (),
						"Created by:\n" +
						"   Eden Weizman\n" +
						"   Connor Basich\n" +
						"   Josh Lappen\n\n" +
						"Design & Data Structures 2011-2012!\n\n" +
						"Our birthday present to Mr. Hazard!! ",
					    "ALL HAIL THE DEVELOPERS",
					    JOptionPane.PLAIN_MESSAGE);
				
			}
			
		});
		/*about.add(searching);
		about.add(sorting);*/
		about.add(animation);
		about.add(developerInfo);
		menuBar.add(file);
		menuBar.add(about);
		setJMenuBar(menuBar);
		validate();
		pack();
		
	}
	
	public Dimension getScreenSize()
	{
		Toolkit toolkit =  Toolkit.getDefaultToolkit ();
		Dimension dim = toolkit.getScreenSize();
		return dim;
	}
	
	public class AboutListener implements ActionListener
	{
		private String type;
		
		public AboutListener(String myType)
		{
			type=myType;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			About about=new About(control, type);
		}
	}
}